package com.example.controller;

import com.example.entity.vo.Order;
import com.example.entity.vo.State;
import com.example.service.OrderService;
import com.example.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Component
public class OrderTask {
    @Autowired
    private OrderService orderService;

    @PostConstruct
    @Scheduled(cron = "0/5 * * * * ?")
    public void orderTimeCheck() {
        List<Order> orders = orderService.queryAll();
        for (Order order : orders) {
            if (order.getOState().equals("making") || order.getOState().equals("news")) {
                if (!(DateUtils.Transfer(order.getAppointmentStart()).getTime() > new Date().getTime())
                        && DateUtils.compare_date_second(order.getAppointmentEnd(), DateUtils.getLastUpdate()) < 0) {
                    orderService.setOnState(order, State.COMPLETED);
                    System.out.println("更新一例订单状态：" + order.getOid());
                }
            }
        }
    }
}
